import request from '@/utils/request'
import { API_CONFIG, API_BASE_URL } from '@/config'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { getToken } from '@/utils/auth'

// 创建专门的卸货统计API请求实例，使用配置系统
const containerRequest = axios.create({
    baseURL: API_BASE_URL,
    timeout: 10000
})

// Request interceptor for containerRequest
containerRequest.interceptors.request.use(
    config => {
        const token = getToken()
        if (token) {
            config.headers['Authorization'] = token
        }
        return config
    },
    error => {
        console.error('请求错误:', error)
        return Promise.reject(error)
    }
)

// Response interceptor for containerRequest
containerRequest.interceptors.response.use(
    response => {
        const { data } = response
        if (data.code === 200 || data.success) {
            return data.data || data
        } else {
            return Promise.reject(new Error(data.msg || data.message || '请求失败'))
        }
    },
    error => {
        console.error('响应错误:', error)
        return Promise.reject(error)
    }
)

// 获取统计数据
export function getDashboardStats() {
    return request({
        url: API_CONFIG.DASHBOARD_STATS,
        method: 'get'
    })
}

// 获取图表数据
export function getDashboardChart() {
    return request({
        url: API_CONFIG.DASHBOARD_CHART,
        method: 'get'
    })
}

// 获取卸货统计数据
export function getUnloadStats() {
    return containerRequest({
        url: API_CONFIG.UNLOAD_STATS,
        method: 'get'
    })
}

// 获取容器数据看板数据
export function getContainerDashboard() {
    return containerRequest({
        url: API_CONFIG.CONTAINER_DASHBOARD,
        method: 'get'
    })
}

// 获取卸货列表数据
export function getUnloadList(params) {
    return containerRequest({
        url: API_CONFIG.CONTAINER_UNLOAD_LIST,
        method: 'get',
        params
    })
} 